package net.jxiny.oschina.tweet.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;

import net.jxiny.oschina.R;
import net.jxiny.oschina.net.URLList;

import net.jxiny.oschina.tweet.Adapter.TopicAdapter;
import net.jxiny.oschina.tweet.Result.TopicResult;
import net.jxiny.oschina.tweet.enyity.Topic;
import net.jxiny.oschina.util.ACache;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class TopicFragment extends Fragment {
    private Unbinder binder;
    @BindView(R.id.recyclerView)
    RecyclerView TopicView;
    private List<Topic> lists = new ArrayList<>();
    private TopicAdapter mTopicAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_topic, container, false);
        binder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Topic topic = new Topic();
        OkHttpUtil.getDefault(this)
                .doGetAsync(HttpInfo.Builder().setUrl(URLList.GET_TOPIC +
                        ACache.get(getActivity()).getAsString("token")+"&catalog=1").build(), new Callback() {
                    @Override
                    public void onSuccess(HttpInfo info) throws IOException {

                        TopicResult result = info.getRetDetail(TopicResult.class);
                        mTopicAdapter.replaceData(result.getTweetlist());
                    }
                    @Override
                    public void onFailure(HttpInfo info) throws IOException {
                        System.out.println(info);
                    }
                });
        //*********************************************
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false);
        TopicView.setLayoutManager(manager);
        mTopicAdapter = new TopicAdapter(R.layout.item_topic,lists);
        TopicView.setAdapter(mTopicAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binder.unbind();
    }
}
